<?php

/**
 * PHP IDS
 * 
 * Requirements: PHP5, SimpleXML, MultiByte Extension/Iconv (optional)
 *
 * Copyright (c) 2007 PHPIDS (http://phpids.org)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the license.
 *
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
	
require_once 'log_interface.php';

/**
 * Callback logger
 *
 * This class provides a callback based logger. You can add callbacks
 * and specify on which impact they should be called. Also a generic
 * callback (default behaviour) is provided.
 *
 * @author	Lars Strojny <lstrojny@neu.de>
 */
class IDS_Log_Callback implements IDS_Log_Interface
{
	/**
	 * List of callbacks
	 *
	 * @var		array
	 * @access	protected
	 */
	protected $_callbacks = array();

	/**
	 * Execute logging
	 *
	 * @access	public
	 * @param	array	$data
	 * @return	object	$this
	 */
	public function execute($data)
	{
		if (empty($this->_callbacks)) {
			throw new Exception('No callback(s) specified');
		}
		foreach ($this->_callbacks as $callback) {
			// General callback
			if ($callback[1] === null) {
				call_user_func_array($callback[0], array($data));
			} else {
				$impact = 0;
				foreach ($data as $row) {
					foreach ($row['filter'] as $filter) {
						if (isset($filter['impact'])) {
							$impact += (int)$new_impact;
						}
					}
				}
				if (version_compare($impact, $callback[1], $callback[2])) {
					call_user_func_array($callback[0], array($data));
				}
			}
		}
		
		return $this;
	}

	/**
	 * Add callback
	 *
	 * Add a certain callback either a global one (when leaving $impact null)
	 * or for specified impact levels.
	 *
	 * @access	public
	 * @param 	callback 		$callback
	 * @param 	void|integer 	$impact
	 * @param 	string			$operator >, <, >= or <=
	 * @return	object			$this or exception on failure
	 */
	public function addCallback($callback, $impact = null, $operator = '>=')
	{
		if (!is_callable($callback)) {
			throw new Exception('Invalid callback given');
		}

		if (!in_array($operator, array('>', '<', '>=', '<='))) {
			throw new Exception(
				'Invalid operator given. Choose one of those: >, <, >=, <='
			);
		}
		$impact = $impact === null ? $impact : (int)$impact;
		$this->_callbacks[] = array($callback, $impact, $operator);
		return $this;
	}
}
/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 */